package struts.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import business.PandAction;

import service.vo.MessageVO;
import struts.form.ActionForm;
import struts.form.FullForm;
import struts.form.XmlBean;

public class ActionServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request,HttpServletResponse response)
	throws IOException,ServletException{
		//获得请求头
		String path = this.getPath(request.getServletPath());
		Map<String,XmlBean> map = (Map<String,XmlBean>)this.getServletContext().getAttribute("struts");
		XmlBean xml = map.get(path);
		String formclass = xml.getFormClass();
		ActionForm form = FullForm.full(formclass, request);
		String actiontype = xml.getActionType();
		Action action = null;
		String url = "";
		try {
			Class clazz = Class.forName(actiontype);
			action = (Action)clazz.newInstance();
			url = action.execute(request, response,form, xml.getActionForwad());		
		} catch (Exception e) {
			System.out.println("严重：控制器异常");
		}
		RequestDispatcher dis  = request.getRequestDispatcher(url);
		dis.forward(request, response);
		/*String url = "";
		Action action = new PandAction();
		url = action.execute(request);
		RequestDispatcher dis = request.getRequestDispatcher(url);
		dis.forward(request, response);*/
	}

	public void doPost(HttpServletRequest request,HttpServletResponse response)
			throws IOException,ServletException{
		this.doGet(request, response);		
	}
	
     private String getPath(String servletPath){
    	 return servletPath.split("\\.")[0];
     }
}
